package com.prs.crm.job.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.action.LoginAction;
import com.prs.crm.domain.cqunicom.OrderChart;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.job.ByDayTask;
import com.prs.crm.service.cqunicom.OrderChartService;
import com.prs.crm.service.cqunicom.OrderService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-4-8 下午03:37:53
 * 简单说明
 */

public class ByDayTaskImpl implements ByDayTask {

	private OrderChart orderChart;
	@Autowired
	private  OrderChartService orderChartService;
	private  OrderService orderService;

	public OrderChart getOrderChart() {
		return orderChart;
	}
	public void setOrderChart(OrderChart orderChart) {
		this.orderChart = orderChart;
	}
	public OrderChartService getOrderChartService() {
		return orderChartService;
	}
	public void setOrderChartService(OrderChartService orderChartService) {
		this.orderChartService = orderChartService;
	}
    public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	/**
     * 按照天數
     */
	public void byDay() {
		System.out.println("每天执行");
		try {
			setByDays();
		} catch (ParseException e) {
			
			e.printStackTrace();
		}

	}
	// 按日加入
	public void setByDays() throws ParseException {
		String currens = setTimeStyleB().format(new Date());
		List<PoplarOrder> orList = this.getOrderService().getCurrentOrder(
				setTimeStyleA().parse(currens + " 00:00:00"),
				setTimeStyleA().parse(currens + " 59:59:59"));
		int serchLength = orList.size();
		List<OrderChart> chartListzj = new ArrayList<OrderChart>();
		List<OrderChart> chartListpd = new ArrayList<OrderChart>();
		if (serchLength > 0) {

			for (PoplarOrder order : orList) {
				OrderChart oc = new OrderChart();

				oc.setDayDate(setTimeStyleB().format(order.getRecordTime())
						+ "");
				oc.setDayHy(order.getServices().getName());
				oc.setDayWord(order.getServices().getServicesType().getName());
				oc.setDayBoNum(1);

				if (order.getBusinessOrder() != null) {
					if ("已成交".equals(order.getBusinessOrder()
							.getTransationSatet())) {
						oc.setDayCJnNum(1);
					} else {
						oc.setDayCJnNum(0);
					}
				} else {
					oc.setDayCJnNum(0);
				}
				oc.setDayCJlv("0%");
				if ("转接".equals(order.getModleName())) {
					oc.setModles("转接");
					oc.setDaySJBoNum(1);
					chartListzj.add(oc);
				} else {
					oc.setModles("派单");
					oc.setDaySJBoNum(0);
					chartListpd.add(oc);
				}
			}

		}
		// 派单处理
		// chartList
		List<OrderChart> chartListpd2 = new ArrayList<OrderChart>();
		for (OrderChart pd : chartListpd) {
			if (chartListpd2.size() == 0) {
				chartListpd2.add(pd);
			} else {
				int j = 0;
				for (int i = 0; i < chartListpd2.size(); i++) {
					String sera = pd.getDayHy();
					String serb = chartListpd2.get(i).getDayHy();

					if (sera.equals(serb)) {
						chartListpd2.get(i).setDayBoNum(
								chartListpd2.get(i).getDayBoNum()
										+ pd.getDayBoNum());
						chartListpd2.get(i).setDayCJnNum(
								chartListpd2.get(i).getDayCJnNum()
										+ pd.getDayCJnNum());
						j++;
					}
				}
				if (j > 0) {

				} else {
					chartListpd2.add(pd);
				}
			}
		}
		// 转接处理
		List<OrderChart> chartListpd1 = new ArrayList<OrderChart>();
		for (OrderChart pd : chartListzj) {
			if (chartListpd1.size() == 0) {
				chartListpd1.add(pd);
			} else {
				int j = 0;
				for (int i = 0; i < chartListpd1.size(); i++) {
					OrderChart ss = chartListpd1.get(i);
					String sera = pd.getDayHy();
					String serb = ss.getDayHy();
					if (sera.equals(serb)) {
						ss.setDayBoNum(ss.getDayBoNum() + pd.getDayBoNum());
						ss.setDayCJnNum(ss.getDayCJnNum() + pd.getDayCJnNum());
						j++;
					}
				}
				if (j == 0) {

					chartListpd1.add(pd);

				}
			}
		}
		// 总和处理
		List<OrderChart> chartListpdPage = new ArrayList<OrderChart>();
		for (OrderChart ss : chartListpd2) {
			if (ss.getDayBoNum() == 0) {
				ss.setDayCJlv("0%");
			} else {
				ss.setDayCJlv((ss.getDayCJnNum() / ss.getDayBoNum()) * 100
						+ "%");
			}
			chartListpdPage.add(ss);
		}
		for (OrderChart ss : chartListpd1) {
			chartListpdPage.add(ss);
		}
		System.out.println("执行了这里："+chartListpdPage.size());
		for (OrderChart oc : chartListpdPage) {
			
			this.getOrderChartService().save(oc);
		}

	}
	public static SimpleDateFormat setTimeStyleA() {
		SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return ddf;
	}

	public static SimpleDateFormat setTimeStyleB() {
		SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd");
		return ddf;
	}
}
